﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.PersistenceObject.Business;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.Cell.Interface;
using WMS.FC.Domain.Service.Material.Interface;
using WMS.FC.Domain.Service.System;

namespace WMS.FC.Domain.Service.Cell
{
    public class AddCellService : ServiceBase, IAddCellService
    {
        protected readonly ICheckCellExistService checkCellExistService;
        protected readonly IGetCellBindBarcodeService getCellBindBarcodeService;
        protected readonly IGetMaterialByBindBarcodeService getMaterialByBindBarcodeService;
        protected readonly ICellRepository cellRepository;

        public AddCellService(
            ILogger<ServiceBase> logger,
            IOptions<WmsSettings> options,
            IMapper mapper,
            ICheckCellExistService checkCellExistService,
            IGetCellBindBarcodeService getCellBindBarcodeService,
            IGetMaterialByBindBarcodeService getMaterialByBindBarcodeService,
            ICellRepository cellRepository)
            : base(logger, options, mapper)
        {
            this.checkCellExistService = checkCellExistService;
            this.getCellBindBarcodeService = getCellBindBarcodeService;
            this.getMaterialByBindBarcodeService = getMaterialByBindBarcodeService;
            this.cellRepository = cellRepository;
        }

        public async Task<ActionResult<CellPO>> AddCell(string cellBarcode, UserInfo userInfo)
        {
            var result = new ActionResult<CellPO>("登录电池");
            try
            {
                var checkCellExistResult = await checkCellExistService.CheckCellExist(cellBarcode, userInfo);
                if (!checkCellExistResult.IsSuccess)
                    return result.Fail(checkCellExistResult);

                if (checkCellExistResult.ErrorCode == ErrorCode.S)
                    result.Data = checkCellExistResult.Data;
                else
                {
                    var getCellBindBarcodeResult = await getCellBindBarcodeService.GetCellBindBarcode(cellBarcode);
                    if (!getCellBindBarcodeResult.IsSuccess)
                        return result.Fail(getCellBindBarcodeResult);

                    var getCellMaterialByBindBarcodeResult = await getMaterialByBindBarcodeService.GetCellMaterialByBindBarcode(getCellBindBarcodeResult.Data!);
                    if (!getCellMaterialByBindBarcodeResult.IsSuccess)
                        return result.Fail(getCellMaterialByBindBarcodeResult);

                    var material = getCellMaterialByBindBarcodeResult.Data!;
                    var cellPO = new CellPO()
                    {
                        WarehouseCode = options.Value.WarehouseCode,
                        Barcode = cellBarcode,
                        MaterialCode = material.Code,
                        TechnologyRouteCode = options.Value.EmptyTrayTechnologyRoute,
                        TechnologyProcessCode = options.Value.EmptyTrayTechnologyProcess,
                        OriginalNgCode = null,
                        NgCode = null,
                        IsRework = false,
                        RankValue = null,
                        ReworkCount = 0,
                        CellPosition = null,
                        TrayBarcode = null,
                    };

                    var count = await cellRepository.Insert(cellPO, userInfo);
                    if (count == 0)
                        return result.DbError("新增电池数据异常");

                    result.Data = cellPO;
                }
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
